package com.ext.aic.apis.attachment;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.ext.aic.apis.ApiDefine;
import com.ext.aic.apis.ApiPaths;
import com.ext.aic.apis.RequestResponse;
import com.ext.aic.beans.response.AttachmentUrlResponse;
import com.hjq.http.EasyHttp;

public class AttachmentUrlRequest {

    private String resourceName;
    private String attachmentId;

    protected void onAttachmentUrlSuccess(AttachmentUrlResponse response, String resourceName, String attachmentId) {

    }

    protected void onAttachmentUrlFail(Throwable throwable, String attachmentId, String resourceName) {

    }

    public void request(Context context, LifecycleOwner lifecycleOwner, String attachmentId, String resourceName) {
        this.resourceName = resourceName;
        this.attachmentId = attachmentId;
        EasyHttp.get(lifecycleOwner)
                .api(new ApiDefine(String.format(ApiPaths.attachmentUrl, attachmentId)))
                .request(new RequestResponse<AttachmentUrlResponse>(context, false) {
                    @Override
                    protected void onSuccess(AttachmentUrlResponse response) {
                        onAttachmentUrlSuccess(response, AttachmentUrlRequest.this.resourceName, AttachmentUrlRequest.this.attachmentId);
                    }

                    @Override
                    public void onHttpFail(Throwable throwable) {
                        onAttachmentUrlFail(throwable, attachmentId, resourceName);
                    }
                });
    }
}
